


import 'dart:async';

import 'package:event_bus/event_bus.dart';



class EventBusUtils {
  static final eventBus = EventBus();// 初始化你的 eventBus 实例

  static void fire<T>(T event) {
    eventBus.fire(event);
  }

  static StreamSubscription<T> listen<T>(T eventType, Function(T) callback) {
    if (eventType == null) {
      throw ArgumentError('eventType and callback cannot be null');
    }
    return eventBus.on<T>().listen(callback);
  }

  static List<StreamSubscription<dynamic>> listenMultiple(List<Type> eventTypeList, Function(dynamic) callback) {
    List<StreamSubscription<dynamic>> subscriptions = [];
    for (var eventType in eventTypeList) {
      subscriptions.add(eventBus.on<dynamic>().where((event) => event.runtimeType == eventType).listen(callback));
    }
    return subscriptions;
  }

  static void removeListener<T>(T eventType, Function(T) callback) {
    if (eventType == null) {
      throw ArgumentError('eventType and callback cannot be null');
    }
    try {
      final subscription = eventBus.on<T>().where((event) => callback(event)).listen(null);
      subscription.cancel();
    } catch (e) {
      print('Failed to remove listener: $e');
    }
  }

  static void removeMultipleListeners(List<StreamSubscription<dynamic>> subscriptions) {
    for (var subscription in subscriptions) {
      try {
        subscription.cancel();
      } catch (e) {
        print('Failed to cancel subscription: $e');
      }
    }
  }
}
